from rest_framework import viewsets
from .models import *
from .serializers import *


# Create your views here.
class CastingViewSet(viewsets.ModelViewSet):
    queryset = Casting.objects.order_by("-id")
    serializer_class = CastingSerializer
    search_fields = [
        "project_number",
        "draw_number",
        "part_name",
    ]
    filterset_fields = {
        "project_number": ["icontains"],
        "draw_number": ["icontains"],
        "part_name": ["icontains"],
        "inspector": ["exact"],
        "inspect_date": ["gte", "lte"],
        "reviewer": ["exact"],
        "review_date": ["gte", "lte"],
    }


class CastingFilmViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        return CastingFilm.objects.filter(project=self.kwargs["casting_pk"]).order_by(
            "-id"
        )

    serializer_class = CastingFilmSerializer
    search_fields = ["examined_area_number", "film_number", "evaluation"]
    filterset_fields = {
        "examined_area_number": ["exact", "icontains"],
        "film_density": ["gte", "lte"],
        "film_number": ["exact", "icontains"],
    }
    
class CastingDefectViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        return CastingDefect.objects.filter(film=self.kwargs["film_pk"]).order_by(
            "id"
        )
        
    serializer_class = CastingDefectSerializer
    search_fields = ["kind","grade"]
    filterset_fields = {
        "kind": ["exact", "icontains"],
        "grade": ["gte", "lte"],
        "x": ["gte", "lte"],
        "y": ["gte", "lte"],
        "w": ["gte", "lte"],
        "h": ["gte", "lte"],
    }